gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\generalp\kernel1.m

    function [k] = kernel1(a,b,ker,arg)
% function [k] = kernel(a,b,ker,arg)
%
% KERNEL computes a dot product of the point a and b after
%  non-linear mapping of these point. The non-linear mapping
%  is determined a argument ker.
%
% Input:
%  a [DxN] N points of dimension D.
%  b [DxM] M points of domension D.
%  ker [string] is given kernel
%       'linear'  - no argument,
%       'quad'    - no argument,
%       'poly'    - arg(1) is degree of polynomial,
%       'rbf'     - arg(1) is sigma,
%       'sigmoid' - arg(1) is scale, arg(2) is offset,
%       'fourier' - arg(1) is degree,
%       'spline'  - no argument.
%  arg [1xP] P arguments of the given kernel.
%           
% Output:
%  k [NxM] dot (kernel) product or kernel matrix.
%

% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz
% Written Vojtech Franc (diploma thesis)
% Modifications
% 14-Nov-2001, V.Franc, returned argument bias removed
% 10-apr-2001 V.Franc, fourier, spline and quad (as defined in SH10) 
%             kernel added
% 22-mar-2001 V.Franc, bias indicator added
% 26-feb-2001 V.Franc


n1=size(a,2);
n2=size(b,2);

if n1 == 1 & n2 == 1,
  k = kernel1(a,b,ker,arg );
else
  k=zeros(n1,n2);
  
  for i=1:n1,
    for j=1:n2,
      k(i,j)=kernel1(a(:,i),b(:,j),ker,arg );
    end
  end
end

return;
  
%------------------------------------------------------
function [k] = kernel1(a,b,ker,arg );

switch lower(ker)
  
 case 'linear'
   k = a'*b;  
 case 'poly'
   k = (a'*b + 1)^arg(1);
 case 'quad'
   k = (a'*b)^2 + a'*b + 1;
 case 'rbf'  
   k = exp(-(a-b)'*(a-b)/(2*arg(1)^2));    
 case 'sigmoid'
   k = tanh(arg(1)*a'*b + arg(2));
 case 'fourier'
   z = sin(arg(1) + 0.5)*2*ones(length(a),1);
   i = find(a-b);
   z(i) = sin(arg(1)+0.5)*(a(i)-b(i))./sin((a(i)-b(i))/2);
   k = prod(z);
 case 'spline'
   z = 1 + a.*b + a.*b.*min(a,b)-((a+b)/2).*(min(a,b)).^2+(1/3)*(min(a,b)).^3;
   k = prod(z);
end

return;